﻿using System;
using System.Data;
using eTMS.Controllers.DanhSachCamThiService;
using System.Collections.Generic;
namespace eTMS.Controllers{
public class DanhSachCamThiController : DanhSachCamThiControllerBase{
	public DanhSachCamThiController()
		: base()
	{
	}
    private DataTable tblRemoved;
    public void LoadAllDSDanhSachCamThi(int MonHoc_ID,int HocKy_ID,int NamHoc_ID)
    {
        Table = dao.SelectAllDSWithSinhVienByMonHocHocKyNamHoc(MonHoc_ID,HocKy_ID,NamHoc_ID).Tables[0];
        AddIndexToTable();
        SinhVienController.AddTenGioiTinh(Table);
        tblRemoved=Table.Clone();
        
    }
    public void AddSinhVienToDanhSach(List<DataRow>lsSinhVienRow,List<int>lsLyDoCamThi,int MonHoc_ID,int HocKy_ID,int NamHoc_ID)
    {
        foreach (DataRow r in lsSinhVienRow)
        {
            if (Table.Select("SinhVien_ID=" + r["SinhVien_ID"].ToString()).Length == 0)
            {
                DataRow nr = Table.NewRow();
                foreach (DataColumn col in r.Table.Columns)
                {
                    if (Table.Columns.Contains(col.ColumnName))
                    {
                        nr[col.ColumnName] = r[col];
                    }
                }
                if (ConvertInt(r["LyDoCamThi_ID"]) > 0)
                {
                    nr["LyDoCamThi_ID"] = r["LyDoCamThi_ID"];
                }
                else
                {
                    if (lsLyDoCamThi.Count > 0)
                    {
                        nr["LyDoCamThi_ID"] = lsLyDoCamThi[0];
                    }
                    else
                    {
                        if (LyDoCamThiController.ListLyDoCamThiStatic.Count > 0)
                        {
                            nr["LyDoCamThi_ID"] = LyDoCamThiController.ListLyDoCamThiStatic[0].LyDoCamThi_ID;
                        }
                    }
                }
                nr["MonHoc_ID"] = MonHoc_ID;
                nr["HocKy_ID"] = HocKy_ID;
                nr["NamHoc_ID"] = NamHoc_ID;
                Table.Rows.Add(nr);
            }
        }
        AddIndexToTable();
    }
    public void RemoveSinhVienFromDanhSach(List<DataRow> lsSinhVienRow)
    {
        foreach (DataRow r in lsSinhVienRow)
        {
            tblRemoved.ImportRow(r);
            Table.Rows.Remove(r);
        }
        AddIndexToTable();
    }
    public void SaveDanhSachCamThi()
    {
        foreach (DataRow r in tblRemoved.Rows)
        {
            if (!r.IsNull("DanhSachCamThi_ID"))
            {
                dao.DeleteByID(ConvertInt(r["DanhSachCamThi_ID"]));
            }
        }
        foreach (DataRow r in Table.Rows)
        {
            DanhSachCamThiEntity o = MapDataRowToObject(r);
            if (o.DanhSachCamThi_ID > 0)
            {
                dao.Update(o);
            }
            else
            {
                o = dao.SaveRet(o);
                r["DanhSachCamThi_ID"] = o.DanhSachCamThi_ID;
            }
        }
    }
}
}
